package com.wenge.datagroup.storage.config;

import com.wenge.datagroup.storage.bean.Field;
import com.wenge.datagroup.storage.bean.Topic;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author Depp
 * @description 初始化各渠道配置信息
 * @date 2022-09-03 10:06
 */
public class InitContext {
    private final static Logger logger = Logger.getLogger(InitContext.class);
    public static Topic topic = new Topic();

    public static void initChannelConfig(String xmlPath, String configfileName) {
        try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(
                    new File(xmlPath + File.separator + configfileName + ".xml")
            );
            Element root = document.getRootElement();

            for (Iterator<Element> iter = root.elementIterator(); iter.hasNext(); ) {
                Element topicElement = iter.next();

                String consumerTopicName = topicElement.attributeValue("consumer_topic_name");
                String sendTopicTame = topicElement.attributeValue("send_topic_tame");
                String tClazz = topicElement.attributeValue("class");
                String desc = topicElement.attributeValue("desc");

                topic.setConsumerTopicName(consumerTopicName);
                topic.setSendTopicName(sendTopicTame);
                topic.setTClazz(tClazz);
                topic.setDesc(desc);

                LinkedList<Field> fieldList = new LinkedList<>();
                for (Iterator<Element> iterator = topicElement.elementIterator(); iterator.hasNext(); ) {
                    Element ele = iterator.next();
                    Field field = new Field();
                    String orgField = ele.attributeValue("org_field");
                    String orgFieldType = ele.attributeValue("org_field_type");
                    String finalField = ele.attributeValue("final_field");
                    String finalFieldType = ele.attributeValue("final_field_type");
                    String format = ele.attributeValue("format");
                    String clazz = ele.attributeValue("class");
                    String methodName = ele.attributeValue("method_name");
                    String defultValue = ele.attributeValue("defult_value");
                    String preValue = ele.attributeValue("pre_value");

                    field.setOrgField(orgField);
                    field.setOrgFieldType(orgFieldType);
                    field.setFinalField(finalField);
                    field.setFinalFieldType(finalFieldType);
                    field.setFormat(format);
                    field.setClazz(clazz);
                    field.setMethodName(methodName);
                    field.setDefultValue(defultValue);
                    field.setPreValue(preValue);
                    fieldList.add(field);
                }
                topic.setFields(fieldList);
            }
            logger.info(configfileName + " 渠道 配置信息加载完成： " + topic);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        }
    }
}
